package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.http.HttpIndex;
import com.zx.kitchen.index.common.mvp.OnResultListener;
import com.zx.kitchen.index.common.utils.BitmapUtils;
import com.zx.kitchen.index.common.utils.ImageUtil;
import com.zx.kitchen.index.common.utils.Toasts;

/**
 * Created by Administrator on 2017/3/10.
 */

public class EditBusinessInfoActivity extends Activity implements View.OnClickListener {

    private static final int ALBUM_IMAGE_ACTIVITY_REQUEST_CODE = 0xd0;
    private static final int ALBUM_IMAGE_ACTIVITY_REQUEST_QRCODE = 0xd1;

    private LinearLayout llSelectLogo;
    private LinearLayout llSelectQrcode;
    private Spinner spDay;
    private Spinner spMonth;
    private RadioGroup rgPrintInfo;
    private RadioButton rbYes;
    private RadioButton rbNo;
    private EditText etBusinessName;

    private EditText etPhone;
    private EditText etTelphone;
    private EditText etAddress;
    private EditText etBusinessDetail;
    private EditText etBusinessActivity;
    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private ImageView imgLogo;
    private ImageView imgQrcode;
    private Bitmap mBitmap;
    private Button btnSave;
    private Uri imageUri;
    private Uri imageQrcodeUri;
    private static String[] mFontArrayStringDay = new String[]{};
    private static String[] mFontArrayStringMonth = new String[]{};
    private ArrayAdapter<String> mArrayAdapter;
    private int mSpDay = 1;
    private int mSpMonth = 1;
    private int isPrint = 1;
    String imgPath = "";
    String qrcodePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_business_info);
        initData();
        initView();
        initListener();

    }

    private void initData() {
        mFontArrayStringDay = new String[24];
        mFontArrayStringMonth = new String[29];

        for (int i = 0; i < mFontArrayStringDay.length; i++) {
            mFontArrayStringDay[i] = i + ":00";
        }
        for (int i = 0; i < mFontArrayStringMonth.length; i++) {
            mFontArrayStringMonth[i] = i + "日";
        }


    }

    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        llSelectLogo = (LinearLayout) findViewById(R.id.ll_select_logo);
        llSelectQrcode = (LinearLayout) findViewById(R.id.ll_select_qrcode);
        spDay = (Spinner) findViewById(R.id.sp_day);
        spMonth = (Spinner) findViewById(R.id.sp_month);
        rgPrintInfo = (RadioGroup) findViewById(R.id.rg_print_info);
        rbYes = (RadioButton) findViewById(R.id.rb_yes);
        rbNo = (RadioButton) findViewById(R.id.rb_no);
        etBusinessName = (EditText) findViewById(R.id.et_business_name);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etTelphone = (EditText) findViewById(R.id.et_telphone);
        etAddress = (EditText) findViewById(R.id.et_address);
        etBusinessDetail = (EditText) findViewById(R.id.et_business_detail);
        etBusinessActivity = (EditText) findViewById(R.id.et_business_activity);
        imgLogo = (ImageView) findViewById(R.id.img_logo);
        imgQrcode = (ImageView) findViewById(R.id.img_qrcode);
        btnSave = (Button) findViewById(R.id.btn_save);

        toolbarTitle.setText(getResources().getString(R.string.baseInfoSetting));
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));

        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayStringDay) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        //        spinner设置监听
        spDay.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSpDay = Integer.parseInt(mFontArrayStringDay[position].replace(":00", ""));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spDay.setAdapter(mArrayAdapter);
        //        seeting_spinner改变了spinner的默认样式
        mArrayAdapter = new ArrayAdapter<String>(this, R.layout.edit_spinner, mFontArrayStringMonth) {
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.edit_spinner_item, parent, false);
                }
                TextView spinnerText = (TextView) convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
                return convertView;
            }
        };
        spMonth.setAdapter(mArrayAdapter);
        //        spinner设置监听
        spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                mSpMonth = Integer.parseInt(mFontArrayStringMonth[position].replace("日", ""));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        rgPrintInfo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_yes:
                        rbYes.setChecked(true);
                        isPrint = 1;

                        break;
                    case R.id.rb_no:
                        rbNo.setChecked(true);
                        isPrint = 0;
                        break;
                }
            }
        });

    }


    private void initListener() {
        toolbarLeft.setOnClickListener(this);
        llSelectLogo.setOnClickListener(this);
        llSelectQrcode.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        toolbarTitle.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.ll_select_logo:
                openAlbum(ALBUM_IMAGE_ACTIVITY_REQUEST_CODE);
                break;
            case R.id.ll_select_qrcode:
                openAlbum(ALBUM_IMAGE_ACTIVITY_REQUEST_QRCODE);
                break;
            case R.id.btn_save:
                imgPath = ImageUtil.getRealFilePath(getBaseContext(), imageUri);
                qrcodePath = ImageUtil.getRealFilePath(getBaseContext(), imageQrcodeUri);
                Log.d("EditBusinessInfoActivit", imgPath);
                if (!TextUtils.isEmpty(etBusinessName.getText().toString()) && !TextUtils.isEmpty(etPhone.getText().toString())) {
                    HttpIndex.saveBusinessInfo(getBaseContext(), imgPath,
                            qrcodePath
                            , etBusinessName.getText().toString().trim(), etTelphone.getText().toString().trim(),
                            etPhone.getText().toString(), etAddress.getText().toString(), mSpDay,
                            mSpMonth, etBusinessActivity.getText().toString(), isPrint, new OnResultListener() {
                                @Override
                                public void onSuccess(int requestCode, String str) {
                                    Log.d("onSuccess========", str);
                                    startActivity(new Intent(EditBusinessInfoActivity.this, BusinessInfoActivity.class));
                                }

                                @Override
                                public void onFailure(int requestCode, String str) {
                                    Toasts.show(getBaseContext(), "保存失败");

                                }
                            }, null);
                } else {
                    Toasts.show(getBaseContext(), "请确认必填项是否输入内容！");
                }

                break;
            case R.id.toolbar_title:
                startActivity(new Intent(this, IndexActivity.class));
                break;

        }

    }


    private void openAlbum(int code) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, code);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {

            case ALBUM_IMAGE_ACTIVITY_REQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    imageUri = data.getData();
                    showImage(imageUri, imgLogo);
                }
                break;
            case ALBUM_IMAGE_ACTIVITY_REQUEST_QRCODE:
                if (resultCode == RESULT_OK) {
                    imageQrcodeUri = data.getData();
                    showImage(imageQrcodeUri, imgQrcode);
                }
                break;
        }
    }

    private void showImage(Uri uri, ImageView imageView) {
        if (mBitmap != null) {
            mBitmap = null;
            System.gc();
        }
        mBitmap = BitmapUtils.getBitmap(uri, this);
        imageView.setImageBitmap(mBitmap);
    }
}
